package cn.iutils.follow.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.iutils.common.Page;
import cn.iutils.common.utils.JStringUtils;
import cn.iutils.common.controller.BaseController;
import cn.iutils.follow.entity.Follow;
import cn.iutils.follow.service.FollowService;

/**
* 关注表 控制器
* @author iutils.cn
* @version 1.0
*/
@Controller
@RequestMapping("${adminPath}/follow")
public class FollowController extends BaseController {

    @Autowired
    private FollowService followService;

    @ModelAttribute
    public Follow get(@RequestParam(required = false) String id) {
        Follow entity = null;
        if (JStringUtils.isNotBlank(id)) {
            entity = followService.get(id);
        }
        if (entity == null) {
            entity = new Follow();
        }
        return entity;
    }

    @RequiresPermissions("follow:view")
    @RequestMapping()
    public String list(Model model, Page<Follow> page) {
        page.setOrderBy("a.create_date desc");
        model.addAttribute("page", page.setList(followService.findPage(page)));
        return "follow/list";
    }

    @RequiresPermissions("follow:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Follow follow,Model model) {
        model.addAttribute("follow", follow);
        return "follow/form";
    }

    @RequiresPermissions("follow:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(Follow follow, RedirectAttributes redirectAttributes) {
        followService.save(follow);
        addMessage(redirectAttributes,"新增成功");
        return "redirect:"+ adminPath +"/follow/update?id="+follow.getId();
    }

    @RequiresPermissions("follow:update")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(Follow follow, Model model) {
        model.addAttribute("follow", follow);
        return "follow/form";
    }

    @RequiresPermissions("follow:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Follow follow, RedirectAttributes redirectAttributes) {
        followService.save(follow);
        addMessage(redirectAttributes,"修改成功");
        return "redirect:"+ adminPath +"/follow/update?id="+follow.getId();
    }

    @RequiresPermissions("follow:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable("id") String id,int pageNo,int pageSize, RedirectAttributes redirectAttributes) {
        followService.delete(id);
        addMessage(redirectAttributes,"删除成功");
        return "redirect:"+ adminPath +"/follow?pageNo="+pageNo+"&pageSize="+pageSize;
    }
}
